package com.spring.dao.person.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.spring.exception.DaoException;
import com.spring.bean.person.PersonBasic;
import com.spring.dao.person.PersonBasicDao;
import com.spring.bean.page.Page;
import com.spring.bean.page.PageView;
import com.spring.dao.DaoSupport;

@Repository
public class PersonBasicDaoImpl extends DaoSupport implements PersonBasicDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	private static final String TABLE_PERSON_BASIC = "person_basic";

	/** insert */
	@Override
	public boolean insert(PersonBasic personBasic) throws DaoException{
		try{
			return insert(jdbcTemplate, personBasic);
		} catch (Exception e) {
			throw new DaoException("PersonBasic --- insert error : " + e.getMessage());
		}
	}

	/** udpate */
	@Override
	public boolean update(PersonBasic personBasic) throws DaoException{
		try{
			return update(jdbcTemplate, personBasic);
		} catch (Exception e) {
			throw new DaoException("PersonBasic --- update error : " + e.getMessage());
		}
	}

	/** delete */
	@Override
	public boolean delete(Integer id) throws DaoException{
		try{
			return deleteById(jdbcTemplate, TABLE_PERSON_BASIC, id);
		} catch (Exception e) {
			throw new DaoException("PersonBasic --- delete error : " + e.getMessage());
		}
	}

	/** getEntityById */
	@Override
	public PersonBasic getById(Integer id) throws DaoException{
		try{
			return getById(jdbcTemplate, PersonBasic.class , id);
		} catch (Exception e) {
			throw new DaoException("PersonBasic --- query by id error : " + e.getMessage());
		}
	}

	/** queryForList */
	@Override
	public List<PersonBasic> queryForList() throws DaoException{
		String sql = "select id,email,name,password,salt,photo,sex,motto,province,city,town,attented_num,attent_num,credits,sign_time,is_able,mark_ad from " + TABLE_PERSON_BASIC;
		try{
			return queryForList(jdbcTemplate, PersonBasic.class, sql);
		} catch (Exception e) {
			throw new DaoException("PersonBasic --- query list error : " + e.getMessage());
		}
	}

	/** queryForPage */
	@Override
	public PageView<PersonBasic> queryForPage(Page page) throws DaoException{
		String sql = "select SQL_CALC_FOUND_ROWS id,email,name,password,salt,photo,sex,motto,province,city,town,attented_num,attent_num,credits,sign_time,is_able,mark_ad from " + TABLE_PERSON_BASIC;
		try{
			return queryForPage(jdbcTemplate, PersonBasic.class, sql, page);
		} catch (Exception e) {
			throw new DaoException("PersonBasic --- query page error : " + e.getMessage());
		}
	}
}